package online.westonline.com.westonline.recivier;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import cn.jpush.android.api.JPushInterface;
import online.westonline.com.westonline.entry.PushDataBean;
import online.westonline.com.westonline.event.PushNotifyEvent;
import online.westonline.com.westonline.ui.MeetRoomStateActivity;

/**
 *接受消息
 * 将通知id和实体保存入Hawk里是最好的
 * Created by xiatao on 2018/8/9.
 */

public class ImportMessageRecivicver extends BroadcastReceiver {
    private int count = 0;
    private String message_content;
    private Bundle my_bunldle = new Bundle();
    @Override
    public void onReceive(Context context, Intent intent) {

        //消息本体内容
        count=count+1;

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            Bundle bundle = intent.getExtras();
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            Log.d("提示", "[MyReceiver] 接收Registration Id : " + regId);
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Bundle bundle = intent.getExtras();
            Log.d("提示", "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            Log.e("消息",bundle.getString(JPushInterface.EXTRA_MESSAGE));
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Bundle bundle = intent.getExtras();
            Log.d("提示", "[MyReceiver] 接收到推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d("提示", "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
            //推送
            EventBus.getDefault().post(new PushNotifyEvent(true));
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            //点击展开
            Log.d("提示", "[MyReceiver] 用户点击打开了通知");
                    Bundle bundle = intent.getExtras();
                    //最原始的信息
                    String initcontent = bundle.getString(JPushInterface.EXTRA_EXTRA);
                    parsedate(initcontent,context,intent);




        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
            Bundle bundle = intent.getExtras();
            Log.d("提示", "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
            //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..

        } else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
            Log.w("提示", "[MyReceiver]" + intent.getAction() +" connected state change to "+connected);
        } else {
            Log.d("提示", "[MyReceiver] Unhandled intent - " + intent.getAction());
        }

        /*
        //提示内容
        String message_alert = bundle.getString(JPushInterface.EXTRA_ALERT);
        //消息标题
        String message_title = bundle.getString(JPushInterface.EXTRA_TITLE);
        //通知栏标题
        String message_notify_title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
        //附加字段
        String extra_data = bundle.getString(JPushInterface.EXTRA_EXTRA);
        */
        //接收message
        //接收到推送
        //去掉反斜杠

    }

    /**
     * 解析推送数据
     * @param content
     * @param context
     * @param intent
     */
    private void parsedate(String content, Context context, Intent intent) {
        //解码
        PushDataBean dataBean = new Gson().fromJson(content,PushDataBean.class);
        Log.d("消息", "[MyReceiver] 用户点击打开了通知");
        //打开自定义的Activity
        //去往会议室界面
        Intent i = new Intent(context,MeetRoomStateActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra("data",dataBean);
        context.startActivity(i);
    }


}
